﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Me.Prettyprint.Cassandra.Utils
{
    public class Metric
    {
        private int _countValue = 0;
        private long startTicks = DateTime.Now.Ticks;

        public Metric(MetricName name)
        {
            Name = name;
        }
        public MetricName Name
        {
            get;
            private set;
        }
        public long ElapsedTicks
        {
            get;
            set;
        }
        public int Count
        {
            get
            {
                return _countValue;
            }
            private set
            {
                Thread.MemoryBarrier();
                this._countValue = value;
                Thread.MemoryBarrier();
            }
        }
        public void IncrementCount()
        {
            Interlocked.Increment(ref this._countValue);
        }
        public void UpdateMetric(long elapsedTime)
        {
            ElapsedTicks += elapsedTime;
            IncrementCount();
        }
        public double MeanTimePerOperation
        {
            get
            {
                return ElapsedTicks / Count;
            }
        }
        public double Rate
        {
            get
            {
                return (double)Count / (double)(DateTime.Now.Ticks - startTicks);
            }
        }
    }
}
